from typing import Generator, Iterator

import pygit2
from pygit2 import GIT_SORT_REVERSE, GIT_SORT_TOPOLOGICAL


class Funcs:
    def get_repo(self, path: str) -> pygit2.Repository:
        repo = pygit2.Repository(path)
        return repo

    def get_commits(self, repo: pygit2.Repository) -> Iterator[pygit2.Commit]:
        for commit in repo.walk(repo.head.target, GIT_SORT_TOPOLOGICAL):
            yield commit

    def list_tag(self, repo: pygit2.Repository):
        import re

        regex = re.compile("^refs/tags/")
        tags = [r for r in repo.references if regex.match(r)]
        return tags
